/**
 *    Copyright 2014 all reserved by Ihome corporate, any persons
 *    or corporate must not use this code for business purpose before
 *    permission is gained.
 *  
 *    ******                                      ******
 *    ******   I Love My Home       ******
 *    ******                                      ******
 *    
 *    Author: DMF
 *    Create Date: Mar 18, 2014
 *
 */
package com.ihome.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;




import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

public class IHCommonDAO implements BaseDAO{
   
	@Resource private SessionFactory sessionFactory;
	
	@Override
	public long countAll(String clazz) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		String hql = "select count(*) from "+clazz+" as a";
		Query query = session.createQuery(hql);
		
		 query.setMaxResults(1);
	     Long result = (Long)query.uniqueResult();
		
		 return result.longValue();
	}

	@Override
	public long countQuery(String hql) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery(hql);
		query.setMaxResults(1);
		Long result = (Long)query.uniqueResult();
		
		return result.longValue();
	}

	@Override
	public void delById(Class clazz, Serializable id) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		session.delete(session.load(clazz, id));
	}

	@Override
	public List listAll(String clazz) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		String hql = "from "+clazz+" as a";
		Query query = session.createQuery(hql);	
		
		return query.list();
	}

	@Override
	public List listAll(String clazz, int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		
		String hql = "from "+clazz+" as a";
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery(hql);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNo-1)*pageSize);
		return query.list();
	}

	@Override
	public Object loadById(Class clazz, Serializable id) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		 return	session.load(clazz, id);
		 
	}

	@Override
	public Object loadObjec(String hql) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		List list = session.createQuery(hql).list();
		
		if(list.size() > 0)
			return list.get(0);
		else
			return
					null;
	}

	@Override
	public List query(String hql) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		return session.createQuery(hql).list();
	}

	@Override
	public List query(String hql, int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery(hql);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNo-1)*pageSize);
		
		List result = query.list();
		
		if(!Hibernate.isInitialized(result))
			Hibernate.initialize(result);
		
		return result;
	}

	@Override
	public void saveOrUpdate(Object obj) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		session.saveOrUpdate(obj);
		
	}

	@Override
	public long update(String hql) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession();
		
		Query query = session.createQuery(hql);
		
		Integer result = (Integer)query.executeUpdate();
		return result.intValue();
	}

}
